package dal

import (
	"context"
	"fmt"
	"lianzi_gos/internal/mods/mpweixin/schema"
	"lianzi_gos/pkg/errors"
	"lianzi_gos/pkg/util"

	"gorm.io/gorm"
)

// Get collect storage instance
func GetCollectDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return util.GetDB(ctx, defDB).Model(new(schema.Collect))
}

// 收藏表
type Collect struct {
	DB *gorm.DB
}

// Query collects from the database based on the provided parameters and options.
func (a *Collect) Query(ctx context.Context, params schema.CollectQueryParam, opts ...schema.CollectQueryOptions) (*schema.CollectQueryResult, error) {
	var opt schema.CollectQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	db := a.DB.Table(fmt.Sprintf("%s AS c", new(schema.Collect).TableName())).
		Joins(fmt.Sprintf("left join %s a on c.product_id=a.id", new(schema.GoodsAttr).TableName())).
		Joins(fmt.Sprintf("left join %s p on a.code=p.code", new(schema.Goods).TableName())).
		Where("a.status=?", "enabled").
		Where("p.status=?", "enabled").
		Select("c.id as coll_id,a.stock,a.b_name,a.id as attr_id,c.product_id,p.price,p.speed,p.address,p.city," +
			"p.province,p.district,p.city,a.name,a.unit,a.fineness,a.size,a.power,a.diameter,a.capacity,a.method,a.year,a.image,a.video_url,a.price,a.price_image")

	if v := params.UID; len(v) > 0 {
		db = db.Where("c.uid = ?", v)
	}
	if v := params.Name; len(v) > 0 {
		db = db.Order("a.name ")
	}

	var list schema.Collects
	pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
	if err != nil {
		return nil, errors.WithStack(err)
	}

	queryResult := &schema.CollectQueryResult{
		PageResult: pageResult,
		Data:       list,
	}
	return queryResult, nil
}

// Get the specified collect from the database.
func (a *Collect) Get(ctx context.Context, id string, opts ...schema.CollectQueryOptions) (*schema.Collect, error) {
	var opt schema.CollectQueryOptions
	if len(opts) > 0 {
		opt = opts[0]
	}

	item := new(schema.Collect)
	ok, err := util.FindOne(ctx, GetCollectDB(ctx, a.DB).Where("id=?", id), opt.QueryOptions, item)
	if err != nil {
		return nil, errors.WithStack(err)
	} else if !ok {
		return nil, nil
	}
	return item, nil
}

// Exist checks if the specified collect exists in the database.
func (a *Collect) Exists(ctx context.Context, id string) (bool, error) {
	ok, err := util.Exists(ctx, GetCollectDB(ctx, a.DB).Where("id=?", id))
	return ok, errors.WithStack(err)
}

// Exist checks if the specified collect exists in the database.
func (a *Collect) ExistsCollect(ctx context.Context, product_id, uid string) (bool, error) {
	ok, err := util.Exists(ctx, GetCollectDB(ctx, a.DB).Where("product_id=?", product_id).Where("uid=?", uid))
	return ok, errors.WithStack(err)
}

// Create a new collect.
func (a *Collect) Create(ctx context.Context, item *schema.Collect) error {
	result := GetCollectDB(ctx, a.DB).Create(item)
	return errors.WithStack(result.Error)
}

// Update the specified collect in the database.
func (a *Collect) Update(ctx context.Context, item *schema.Collect) error {
	result := GetCollectDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
	return errors.WithStack(result.Error)
}

// Delete the specified collect from the database.
func (a *Collect) Delete(ctx context.Context, id, uid string) error {
	result := GetCollectDB(ctx, a.DB).Where("product_id=?", id).Where("uid=?", uid).Delete(new(schema.Collect))
	return errors.WithStack(result.Error)
}
